package com.yikaiye.android.yikaiye.ui.adapter;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.databinding.ItemGsChangeContentBinding;
import com.yikaiye.android.yikaiye.databinding.ItemGsChangeTopDateBinding;
import com.yikaiye.android.yikaiye.model.company.GsChangeRecordDataModel;
import com.yikaiye.android.yikaiye.utils.DensityUtil;
import com.yikaiye.android.yikaiye.utils.StrCompareColorTool;

import java.util.List;

/**
 * 工商变更
 */
public class GsChangeAdapter extends BaseQuickAdapter<GsChangeRecordDataModel.ChangeRecordDataModel, DataBindingHolder<ItemGsChangeTopDateBinding>> {

    private final Context context;

    public GsChangeAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemGsChangeTopDateBinding> itemGsChangeTopDateBindingDataBindingHolder, int position, @Nullable GsChangeRecordDataModel.ChangeRecordDataModel changeRecordDataModel) {
        ItemGsChangeTopDateBinding binding = itemGsChangeTopDateBindingDataBindingHolder.getBinding();
        if (changeRecordDataModel != null) {
            MyAdapter adapter = new MyAdapter(position, getItemCount());
            adapter.submitList(changeRecordDataModel.getChangeRecords());
            binding.recyclerView.setAdapter(adapter);
            binding.tvDate.setText(changeRecordDataModel.getDate());
            binding.topLine.setVisibility(position == 0 ? View.INVISIBLE : View.VISIBLE);
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemGsChangeTopDateBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_gs_change_top_date, viewGroup);
    }

    class MyAdapter extends BaseQuickAdapter<GsChangeRecordDataModel.ChangeRecordModel, DataBindingHolder<ItemGsChangeContentBinding>> {

        private List<GsChangeRecordDataModel.ChangeRecordModel> mData;
        private final int parentDataCount;
        private final int parentPosition;

        public MyAdapter(int parentPosition, int parentDataCount) {
            this.parentDataCount = parentDataCount;
            this.parentPosition = parentPosition;
        }

        @Override
        protected void onBindViewHolder(@NonNull DataBindingHolder<ItemGsChangeContentBinding> itemGsChangeContentBindingDataBindingHolder, int position, @Nullable GsChangeRecordDataModel.ChangeRecordModel changeRecordModel) {
            ItemGsChangeContentBinding binding = itemGsChangeContentBindingDataBindingHolder.getBinding();
            if (changeRecordModel != null) {
                binding.tvChangeTitle.setText(changeRecordModel.getChangeItem());
                if (TextUtils.isEmpty(changeRecordModel.getAfterContent()) && TextUtils.isEmpty(changeRecordModel.getBeforeContent())) {
                    binding.tvChangeAfter.setText("无");
                    binding.tvChangeBefore.setText("无");
                }
                if (TextUtils.isEmpty(changeRecordModel.getAfterContent()) && !TextUtils.isEmpty(changeRecordModel.getBeforeContent())) {
                    binding.tvChangeAfter.setText("无");
                    String text = String.format(context.getString(R.string.gs_change_content), changeRecordModel.getBeforeContent());
                    binding.tvChangeBefore.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY));
                }
                if (TextUtils.isEmpty(changeRecordModel.getBeforeContent()) && !TextUtils.isEmpty(changeRecordModel.getAfterContent())) {
                    String text = String.format(context.getString(R.string.gs_change_content), changeRecordModel.getAfterContent());
                    binding.tvChangeAfter.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY));
                    binding.tvChangeBefore.setText("无");
                }

                String s1;
                String s2;
                if (!TextUtils.isEmpty(changeRecordModel.getBeforeContent()) && !TextUtils.isEmpty(changeRecordModel.getAfterContent())) {
                    if (changeRecordModel.getAfterContent().length() > changeRecordModel.getBeforeContent().length()) {
                        s1 = StrCompareColorTool.compareStrShort(changeRecordModel.getAfterContent(), changeRecordModel.getBeforeContent(), ContextCompat.getColor(context, R.color.color_f93f50));
                        s2 = StrCompareColorTool.compareStrLong(changeRecordModel.getAfterContent(), changeRecordModel.getBeforeContent(), ContextCompat.getColor(context, R.color.color_f93f50));
                    } else {
                        s1 = StrCompareColorTool.compareStrLong(changeRecordModel.getAfterContent(), changeRecordModel.getBeforeContent(), ContextCompat.getColor(context, R.color.color_f93f50));
                        s2 = StrCompareColorTool.compareStrShort(changeRecordModel.getAfterContent(), changeRecordModel.getBeforeContent(), ContextCompat.getColor(context, R.color.color_f93f50));
                    }

                    binding.tvChangeBefore.setText(Html.fromHtml(s1, Html.FROM_HTML_MODE_LEGACY));
                    binding.tvChangeAfter.setText(Html.fromHtml(s2, Html.FROM_HTML_MODE_LEGACY));
                }
//                binding.llLine.setPadding(0, 0, 0, parentPosition == data.size() - 1 && position == mData.size() - 1 ? DensityUtil.dip2px(20) : 0);
                binding.llLine.setPadding(0, 0, 0, parentPosition == parentDataCount - 1 && position == getItemCount() - 1 ? DensityUtil.dip2px(20) : 0);
            }
        }

        @NonNull
        @Override
        protected DataBindingHolder<ItemGsChangeContentBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
            return new DataBindingHolder<>(R.layout.item_gs_change_content, viewGroup);
        }
    }
}
